#include "../inc/weather_check.h"
#include "../inc/forecast.h"
#include "../inc/cJSON.h"

char *httprequest(char *city)
{
    static char request[1024];

    snprintf(request, 1024, "GET /v3/weather/now.json?key=SAewqnjWlC7dvMLfL&location=%s&language=zh-Hans&unit=c HTTP/1.1\r\n"
                            "Host:api.seniverse.com\r\n\r\n",
             strtok(city, "\n"));

    return request;
}

void parse(char *res, int *ok, int *len)
{
    char *retcode = res + strlen("HTTP/1.x ");

    switch (atoi(retcode))
    {
    case 200 ... 299:
        *ok = 1;
        printf("查询成功\n");
        break;

    case 400 ... 499:
        *ok = 0;
        printf("客户端错误\n");
        exit(0);

    case 500 ... 599:
        *ok = 0;
        printf("服务端错误\n");
        exit(0);
    }

    char *p;
    if (p = strstr(res, "Content-Length: "))
    {
        *len = atoi(p + strlen("Content-Length: "));
    }
    printf("https-------------------------\n");
}


char *Get_Weather(char city[20])
{
    // 利用DNS服务查询指定域名的IP
    struct hostent *he = gethostbyname("api.seniverse.com");
    if (he == NULL)
    {
        perror("DNS查询失败");
        exit(0);
    }
    printf("IP: %s\n", inet_ntoa(*(struct in_addr *)((he->h_addr_list)[0])));

    struct sockaddr_in addr;
    socklen_t len = sizeof(addr);
    bzero(&addr, len);

    addr.sin_family = AF_INET;
    addr.sin_addr = *(struct in_addr *)((he->h_addr_list)[0]);
    addr.sin_port = htons(80);
    // 创建TCP套接字(因为HTTP是基于TCP的)，并发起连接
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    if (connect(fd, (struct sockaddr *)&addr, len) == 0)
    {
        printf("连接服务器成功！\n");
    }

    // 准备好HTTP的请求报文
    char *s = httprequest(city);
    write(fd, s, strlen(s));

    // 接收对方的响应头部
    char res[1024];
    int total = 0;
    while (1)
    {
        int n = read(fd, res + total, 1);

        if (n <= 0)
        {
            perror("读取HTTP头部失败");
            exit(0);
        }

        total += n;

        if (strstr(res, "\r\n\r\n"))
            break;
    }

    // 分析响应码，并获取正文的长度
    int ok, jsonlen = 0;
    parse(res, &ok, &jsonlen);
    if (!ok || jsonlen == 0)
    {
        exit(0);
    }
    printf("Get_Weather1111------------------------\n");
    // 接收正文
    char *json = calloc(1, jsonlen);
    total = 0;
    while (jsonlen > 0)
    {
        int n = read(fd, json + total, jsonlen);
        total += n;
        jsonlen -= n;
    }
    printf("Get_Weather2222------------------------\n");
    cJSON *root = cJSON_Parse(json);
    cJSON *body = cJSON_GetObjectItem(root, "results");

    cJSON *a = cJSON_GetArrayItem(body, 0);

    cJSON *now = cJSON_GetObjectItem(a, "now");
    printf("Get_Weather3333------------------------\n");
    // printf("天气情况: %s\n", cJSON_GetObjectItem(now, "text")->valuestring);
    // printf("当前气温: %s°C\n", cJSON_GetObjectItem(now, "temperature")->valuestring);
    // 把天气情况和当前气温打包成一个字符串换行分隔
    char *str = calloc(1, 100);
    printf("Get_Weather4444------------------------\n");
    sprintf(str, "%s\n天气情况: %s\n当前气温: %s°C\n", city, cJSON_GetObjectItem(now, "text")->valuestring, cJSON_GetObjectItem(now, "temperature")->valuestring);

    // printf("Get_Weather4444------------------------\n");
    // strcat(str, city);
    // strcat(str, "\n天气情况: ");
    // strcat(str, cJSON_GetObjectItem(now, "text")->valuestring);
    // strcat(str, "\n当前气温: ");
    // strcat(str, cJSON_GetObjectItem(now, "temperature")->valuestring);
    // strcat(str, "°C\n");

    printf("Get_Weather5555------------------------\n");
    free(json);
    return str;
}